import java.util.ArrayList;
import java.util.List;

/**
 * @author LKQ
 * @date 2022/2/19 11:37
 * @description dfs回溯
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.combinationSum(new int[]{2,3,6,7}, 7);
    }
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> ans = new ArrayList<>();
        List<Integer> combine = new ArrayList<>();
        dfs(candidates, target, ans, combine, 0);
        return ans;
    }
    public void dfs(int[] candidates, int target, List<List<Integer>> ans, List<Integer> combine
                    , int idx) {
        if (idx == candidates.length) {
            return;
        }
        if (target == 0) {
            ans.add(new ArrayList<>(combine));
            return;
        }
        // 直接跳过选择下一个
        dfs(candidates, target, ans, combine, idx+ 1);
        if (target - candidates[idx] >=0) {
            combine.add(candidates[idx]);
            dfs(candidates, target - candidates[idx], ans, combine, idx);
            combine.remove(combine.size() - 1);
        }
    }
}
